// Open Source: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Project: YoProgramo.NET (http://code.google.com/p/yoprogramo/)
// Autor: Fabian Figueredo (info@fabianfigueredo.com.ar)
// Blog: blog.yoprogramo.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using YoProgramo.Domain.BusinessLogic.Service;
using YoProgramo.Domain.Core.Interfaces;
using YoProgramo.Infrastructure.Tools.PagedList;

namespace YoProgramo.Web.Controllers
{
    public class TagController : Controller
    {

        public TagController(ITagRepository repository, ITagService service, IStoryService storyService )
        {
            Repository = repository;
            Service = service;
            StoryService = storyService;
        }

        public ITagRepository Repository { get; set; }
        public ITagService Service { get; set; }
        public IStoryService StoryService { get; set; }


        public ActionResult Index()
        {
            return View();
        }

        public ActionResult View(string slug, int? page)
        {
            ITag tag = Service.GetBySlug(slug);
            if (tag == null)
                return RedirectToRoute("Default", new { controller = "Home", action = "Index", id = "" });

            ViewData["Title"] = "Tag: " + Server.HtmlEncode(tag.Name);
            ViewData["UrlPaging"] = Url.RouteUrl("ViewTagItemsPaging", new { slug = slug, page = "__PAGENUM__" });

            var storyList = (IPagedList<IStory>)StoryService.GetByTag(tag).ToPagedList(page ?? 0, 20);
            return View( storyList);
        }

        public ActionResult Create()
        {
            return View();
        } 
        
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

 
        public ActionResult Edit(int id)
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
